package com.gzy.net_app.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.gzy.net_app.R;
import com.gzy.net_app.bean.ImageInfo;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : guzy
 * @program: net-app
 * @description:
 * @date : 2022-04-02 15:59
 **/
public class ImageInfoAdapter extends RecyclerView.Adapter<ImageInfoListViewHolder> {
  List<ImageInfo> imageInfoList = new ArrayList<>();
  Context context;
  private OnItemClickLitener mOnItemClickLitener;

  public ImageInfoAdapter(Context context) {
    this.context = context;
  }

  public void updateImageInfoList(List<ImageInfo> imageInfoList) {
    this.imageInfoList = imageInfoList;
    notifyDataSetChanged();
  }

  @NonNull @Override public ImageInfoListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.gallery_cell, parent, false);
    return new ImageInfoListViewHolder(view);
  }

  @Override public void onBindViewHolder(@NonNull ImageInfoListViewHolder holder, int position) {

    // holder.getShimmerLayout().setShimmerColor(0x55FFFFFF);
    // holder.getShimmerLayout().setShimmerAngle(15);
    // holder.getShimmerLayout().setShimmerAnimationDuration(2000);
    // holder.getShimmerLayout().startShimmerAnimation();

    Log.d("图片地址：", imageInfoList.get(position).getImageUrl());
    Glide.with(context)
        .load(imageInfoList.get(position).getImageUrl())
        .placeholder(R.drawable.ic_baseline_insert_photo_24)
        .into(holder.getImageView());

    holder.getTvTitle().setText(imageInfoList.get(position).getImageText());
    // holder.getTvTime().setText(imageInfoList.get(position).getPublishTime());
    String pu = imageInfoList.get(position).getPublishStatus();

    //获取底部导航图标颜色，根据图标颜色设置文字颜色
    Resources resource = context.getResources();
    if (pu.equals("public")) {
      holder.getTvGong().setText("公开");
      holder.getTvGong().setTextColor(resource.getColorStateList(R.color.blue));
    } else if (pu.equals("private")) {
      holder.getTvGong().setText("私有");
      holder.getTvGong().setTextColor(resource.getColorStateList(R.color.red));
    } else {
      holder.getTvGong().setText(pu);
      holder.getTvGong().setTextColor(resource.getColorStateList(R.color.yellew));
    }

    holder.itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mOnItemClickLitener.onItemClick(position);
      }
    });
  }

  @Override public int getItemCount() {
    return imageInfoList.size();
  }

  public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
    this.mOnItemClickLitener = mOnItemClickLitener;
  }

  public interface OnItemClickLitener {
    void onItemClick(int position);
  }
}

class ImageInfoListViewHolder extends RecyclerView.ViewHolder {
  ImageView imageView;
  TextView tvTitle, tvGong;
  // ShimmerLayout shimmerLayout;

  // TextView tvTime;

  public ImageInfoListViewHolder(@NonNull View itemView) {
    super(itemView);
    // shimmerLayout = itemView.findViewById(R.id.shimmerLayoutCell);
    imageView = itemView.findViewById(R.id.item_poto_imageView);
    tvTitle = itemView.findViewById(R.id.item_photo_title);
    tvGong = itemView.findViewById(R.id.item_photo_gong);
    // tvTime = itemView.findViewById(R.id.item_photo_time);
  }

  // public ShimmerLayout getShimmerLayout() {
  //   return shimmerLayout;
  // }
  //
  // public void setShimmerLayout(ShimmerLayout shimmerLayout) {
  //   this.shimmerLayout = shimmerLayout;
  // }

  //
  // public TextView getTvTime() {
  //   return tvTime;
  // }
  //
  // public void setTvTime(TextView tvTime) {
  //   this.tvTime = tvTime;
  // }

  public ImageView getImageView() {
    return imageView;
  }

  public void setImageView(ImageView imageView) {
    this.imageView = imageView;
  }

  public TextView getTvTitle() {
    return tvTitle;
  }

  public void setTvTitle(TextView tvTitle) {
    this.tvTitle = tvTitle;
  }

  public TextView getTvGong() {
    return tvGong;
  }

  public void setTvGong(TextView tvGong) {
    this.tvGong = tvGong;
  }
}